<?php
date_default_timezone_set('Asia/Ho_Chi_Minh');
function loadPage($pagename){
	return $_SERVER['PHP_SELF']."?choose=".$pagename;
}
function cutstring($str)
{
	$str=substr($str,0,100);
	return $str;
}
function fewchars($s, $lenght) 
{
	
  if ($s=='' || $s==NULL) return $s;
if (is_array($s)) return $s;
$s = strip_tags(trim($s));
if(strlen($s) >= $lenght  && ($espacio = strpos($s, " ", $lenght ))) {
$s = substr($s, 0, $espacio).'...';
}
return $s;
}
function format($str,$isComma=1){
	
	// 1 as visible comma, 0 as not comma
	// default as 1
	if($isComma==1){
		if (get_magic_quotes_gpc()==false) return "'".mysql_real_escape_string($str)."',";
		else return "'".$str."',";
	}
	elseif($isComma==4){
		return "'".htmlspecialchars($str, ENT_QUOTES)."',";
	}
	elseif($isComma==3){
		return "'".htmlspecialchars($str, ENT_QUOTES)."'";
	}
	else 
	{
	if (get_magic_quotes_gpc()==false) return "'".mysql_real_escape_string($str)."'";
	else return "'".$str."'";
	}
}
function isEmpty($str){
	if($str==null || $str==' '){
		return 1;
	}else return 0;
}
function convert($str) 
{
	$chars = array(
          ''  =>  array('"','\'','  ',' -','- ',':','?','~','!','@','#','$','%','*',';',',','.','&'),
		 
        '-' =>  array(' ','  ','#','@','!','#','-','/'),
		'a'	=>	array('ấ','ầ','ẩ','ẫ','ậ','Ấ','Ầ','Ẩ','Ẫ','Ậ','ắ','ằ','ẳ','ẵ','ặ','Ắ','Ằ','Ẳ','Ẵ','Ặ','á','à','ả','ã','ạ','â','ă','Á','À','Ả','Ã','Ạ','Â','Ă','A'),
		'e' =>	array('ế','ề','ể','ễ','ệ','Ế','Ề','Ể','Ễ','Ệ','é','è','ẻ','ẽ','ẹ','ê','É','È','Ẻ','Ẽ','Ẹ','Ê','E'),
		'i'	=>	array('í','ì','ỉ','ĩ','ị','Í','Ì','Ỉ','Ĩ','Ị','I'),
		'o'	=>	array('ố','ồ','ổ','ỗ','ộ','Ố','Ồ','Ổ','Ô','Ộ','ớ','ờ','ở','ỡ','ợ','Ớ','Ờ','Ở','Ỡ','Ợ','ó','ò','ỏ','õ','ọ','ô','ơ','Ó','Ò','Ỏ','Õ','Ọ','Ô','Ơ','O'),
		'u'	=>	array('U','ứ','ừ','ử','ữ','ự','Ứ','Ừ','Ử','Ữ','Ự','ú','ù','ủ','ũ','ụ','ư','Ú','Ù','Ủ','Ũ','Ụ','Ư'),
		'y'	=>	array('ý','ỳ','ỷ','ỹ','ỵ','Ý','Ỳ','Ỷ','Ỹ','Ỵ','Y'),
		'd'	=>	array('đ','Đ','D'),
		'b'=>array('B'),
		'c'=>array('C'),
		'f'=>array('F'),
		'g'=>array('G'),
		'h'=>array('H'),
		'j'=>array('J'),
		'k'=>array('K'),
		'l'=>array('L'),
		'm'=>array('M'),
		'n'=>array('N'),
		'p'=>array('P'),
		'q'=>array('Q'),
		'r'=>array('R'),
		's'=>array('S'),
		't'=>array('T'),
		'v'=>array('V'),
		'x'=>array('X'),
		'z'=>array('Z'),
		'w'=>array('W'),
 	);

	foreach ($chars as $key => $arr) 
		foreach ($arr as $val)
			$str = str_replace($val,$key,$str);
	return $str;
}
function checkYahoo($nick){
	$filename="http://opi.yahoo.com/online?u=$nick&m=t&t=1";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL,$filename);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
	curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
	$data = curl_exec($ch);
	if ($data=='') return false;
	else{
	if($data=='00')
		return false;
	else
		return	true;
	}
}
function checkSkype($nick){
	$filename="http://mystatus.skype.com/$nick.num";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL,$filename);
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
	curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
	$data = curl_exec($ch);
	if ($data=='') return false;
	else{
	if($data=='1')
		return false;
	else
		return	true;
	}		
}
	function getUrlThumbnail($str){
		return str_replace($search = '/userupload/images/', $replace = '/userupload/_thumbs/Images/', $str); 
}
	function formatMoney($number, $fractional=false) {
    if ($fractional) {
        $number = sprintf('%.2f', $number);
    }
    while (true) {
        $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
        if ($replaced != $number) {
            $number = $replaced;
        } else {
            break;
        }
    }
    return $number;
} 
 	function getCurrentDayString() {
		return date("Y-m-d h:i:s");
	}
	//*******Tạo thumb nail******88////
	
	function mrk($filename,$dest="uploads"){
		if(strpos($filename,"/")==0) $filename=substr($filename,1);
		$dir=explode("/",$filename);
		for($i=0;$i<count($dir)-1;$i++)
		{
			$c_dir=$c_dir."/".$dir[$i];
			$f_dest=$dest.$c_dir."/";
			//echo $f_dest;die;
			if(!is_dir($f_dest)){
				mkdir($f_dest,0777);
				//echo'aa';die;
			}
		}
		return $f_dest.$dir[count($dir)-1];
	}
	
	function make_thumb($img_name,$new_w=0,$new_h=0,$filename="uploads")
 	{
	if($new_w!=0&&$new_h==0) $new_h=caotheorong($img_name,$new_w);
	if($new_h!=0&&$new_w==0) $new_w=caotheorong($img_name,$new_h);
	 $filename=mrk($img_name,$filename);
     $ext=getExtension($img_name);
     if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
         $src_img=imagecreatefromjpeg($img_name);

      if(!strcmp("png",$ext))
         $src_img=imagecreatefrompng($img_name);

     $old_x=imageSX($src_img);
     $old_y=imageSY($src_img);

     $ratio1=$old_x/$new_w;
     $ratio2=$old_y/$new_h;
     if($ratio1>$ratio2)    {
         $thumb_w=$new_w;
         $thumb_h=$old_y/$ratio1;
     }
     else    {
         $thumb_h=$new_h;
         $thumb_w=$old_x/$ratio2;
     }

      // we create a new image with the new dimmensions
     $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

     // resize the big image to the new created one
     imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

     // output the created image to the file. Now we will have the thumbnail into the file named by $filename
     if(!strcmp("png",$ext))
         imagepng($dst_img,$filename); 
     else
         imagejpeg($dst_img,$filename); 

      //destroys source and destination images. 
     imagedestroy($dst_img); 
     imagedestroy($src_img); 
 }

 // This function reads the extension of the file. 
 // It is used to determine if the file is an image by checking the extension. 
	 function getExtension($str) {
			$duoi= explode(".",$str); 
			$type = $duoi[count($duoi)-1];
			return $type;
	 }
	 function caotheorong($src,$rong) {
		$size=getimagesize($src);
		$rong_img=$size[0];
		$cao_img=$size[1];
		$phantram=($rong/$rong_img)*100;
		$cao_new=$cao_img*$phantram;	
			return $cao_new;
	}
	function rongtheocao($src,$cao) {
		$size=getimagesize($src);
		$rong_img=$size[0];
		$cao_img=$size[1];
		$phantram=($cao/$cao_img)*100;
		$rong_new=$rong_img*$phantram;	
			return $cao_new;
	}
	/////***End make thumb*****//////
	function get_table($str){
		$str=explode("_",$str);
		for($i=0;$i<count($str)-1;$i++){
			$table=$table."_".$str[$i];
		}
		return (substr($table,1));
	}
?>